package dyyx.guide.ser;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class SerTest{
	

	public static void main(String[] args) throws Exception {
		
		System.out.println("SerTest start  "+new Date());
		
		UserInfo userInfo = new UserInfo();
		
		userInfo.id = 9;
		userInfo.name = "hello,tiger";
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(userInfo);
		oos.flush();
		oos.close();
		byte[] bytes = baos.toByteArray();
		
		byte[] bytes2 = userInfo.toBytes();
		// 95
		System.out.println("jdk  Serializable bytes size "+bytes.length);
		// 19
		System.out.println("user Serializable bytes size "+bytes2.length);

		////  序列化性能对比
		int runcount = 1000000;
        System.out.println("runcount "+runcount);

		long start = System.currentTimeMillis();
		for(int i=0;i<runcount;i++){
			baos = new ByteArrayOutputStream();
		    oos = new ObjectOutputStream(baos);
			oos.writeObject(userInfo);
			oos.flush();
			oos.close();
			baos.toByteArray();
			baos.close();
		}
		long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println("jdk  time "+time);
        
        //  
        
        start = System.currentTimeMillis();
		baos = new ByteArrayOutputStream();
		for(int i=0;i<runcount;i++){
			baos.reset();
		    oos = new ObjectOutputStream(baos);
			oos.writeObject(userInfo);
			oos.flush();
			oos.close();
			baos.toByteArray();
		}
		end = System.currentTimeMillis();
        time = end - start;
        System.out.println("jdk  time2 "+time);
        
        
        start = System.currentTimeMillis();
		for(int i=0;i<runcount;i++){
			userInfo.toBytes();
		}
	    end = System.currentTimeMillis();
        time = end - start;
        System.out.println("user time "+time);

		
		
	}
}